//
//  SetUpViewController.m
//  Korea
//
//  Created by 崔剑 on 15/9/6.
//  Copyright (c) 2015年 KoreaGo. All rights reserved.
//

#import "SetUpViewController.h"
#import "PersonalViewController.h"
#import "FeedbackViewController.h"
#import "AboutUsViewController.h"
#import "Header.h"
#import "SetUpCell.h"
#import "LogOutCell.h"

@interface SetUpViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *setUpTableView;

@end

@implementation SetUpViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"设置";
    self.view.backgroundColor = [UIColor colorWithRGB:0xf3f4f8 alpha:1];
    
    self.setUpTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 330) style:UITableViewStylePlain];
    self.setUpTableView.backgroundColor = [UIColor colorWithRGB:0xf3f4f8 alpha:1];
    
    // 隐藏滑动条
    self.setUpTableView.showsVerticalScrollIndicator = NO;
    self.setUpTableView.scrollEnabled = NO;
    self.setUpTableView.delegate = self;
    self.setUpTableView.dataSource = self;
    [self.view addSubview:self.setUpTableView];
    // 自适应屏幕
    self.edgesForExtendedLayout = UIRectEdgeNone;
    // 注册重用池
    [self.setUpTableView registerClass:[SetUpCell class] forCellReuseIdentifier:@"setUpReuse"];
    
    [self.setUpTableView registerClass:[LogOutCell class] forCellReuseIdentifier:@"logOutReuse"];
    
    // 返回键
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];

    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 50;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 5;
    }
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 10;
    }
    
    return 20;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        SetUpCell *cell = [tableView dequeueReusableCellWithIdentifier:@"setUpReuse"];
        // 点击cell不变色
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (indexPath.row == 0) {
            cell.listLabel.text = @"   清除缓存";
        } if (indexPath.row == 1) {
            cell.listLabel.text = @"   评价";
        } if (indexPath.row == 2) {
            cell.listLabel.text = @"   意见反馈";
        } if (indexPath.row == 3) {
            cell.listLabel.text = @"   关于我们";
        } if (indexPath.row == 4) {
            cell.listLabel.text = @"   版本信息";
        }

        return cell;
    }
    
    LogOutCell *cell = [tableView dequeueReusableCellWithIdentifier:@"logOutReuse"];
    // 点击cell不变色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && indexPath.row == 0) {
        
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"是否清除缓存" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
        [alertView show];
        
    } if (indexPath.row == 1) {
        
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps ://itunes.apple.com/gb/app/yi-dong-cai-bian/id391945719?mt=8"]];
    } if (indexPath.row == 2) {
        FeedbackViewController *feedbackVC = [[FeedbackViewController alloc] init];
        [self.navigationController pushViewController:feedbackVC animated:YES];
        
    } if (indexPath.row == 3) {
        AboutUsViewController *aboutUsVC = [[AboutUsViewController alloc] init];
        [self.navigationController pushViewController:aboutUsVC animated:YES];
    } if (indexPath.row == 4) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"当前版本" message:@"已是最新版本" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alertView show];
    } if (indexPath.section == 1) {
        
        // 退出登录
        
        
    }
    
    
    
}

// 清除缓存
- (void)clearCache:(NSString *)path {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles = [fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
            [fileManager removeItemAtPath:absolutePath error:nil];
        }
    }
}
// 计算单个文件大小
- (CGFloat)fileSizeAtPath:(NSString *)path {
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        long long size = [fileManager attributesOfItemAtPath:path error:nil].fileSize;
        return size / 1024.0 / 1024.0;
    }
    return 0;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (0 == buttonIndex) {
        alertView.hidden = YES;
    }else{
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        NSString *modelPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
        NSString *cashesPath = [NSString stringWithFormat:@"%@/Caches", modelPath];
        [self clearCache:cashesPath];
        [self.setUpTableView reloadData];
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    }
}

// 计算目录大小
- (CGFloat)folderSizeAtPath:(NSString *)path {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    float folderSize;
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles = [fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
            folderSize += [self fileSizeAtPath:absolutePath];
        }
    }
    return folderSize;
}

- (void)leftAction:(UIBarButtonItem *)button
{
    // 返回
    [self.navigationController popToRootViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
